home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH16 / MATCHFNC.ASM < prev    next >
Encoding:
Assembly Source File  |  1994-07-10  |  3.3 KB  |  164 lines

  1.         .xlist
  2.  
  3.         include     stdlib.a
  4.         includelib    stdlib.lib
  5.         matchfuncs
  6.         .list
  7.  
  8.  
  9.  
  10. dseg        segment    para public 'data'
  11.  
  12. TestString    byte    "This is the string 'xyz' in it",cr,lf,0
  13.  
  14. TestPat        pattern    {matchtoistr,xyz}
  15. xyz        byte    "XYZ",0
  16.  
  17. dseg        ends
  18.  
  19.  
  20.  
  21.  
  22.  
  23. cseg        segment    para public 'code'
  24.         assume    cs:cseg, ds:dseg
  25.  
  26. ; MatchToiStr-    Matches all characters in a string up to, and including, the
  27. ;        specified parameter string.  The parameter string must be
  28. ;        all upper case characters.  This guy matches string using
  29. ;        a case insensitive comparison.
  30. ;
  31. ; inputs:
  32. ;        es:di-    Source string
  33. ;        ds:si-    String to match
  34. ;        cx-     Maximum match position
  35. ;
  36. ; outputs:
  37. ;        ax-    Points at first character beyond the end of the matched
  38. ;            string if success, contains the initial DI value if
  39. ;            failure occurs.
  40. ;        carry-    0 if failure, 1 if success.
  41.  
  42.  
  43. MatchToiStr    proc    far
  44.         pushf
  45.         push    di
  46.         push    si
  47.         cld
  48.  
  49. ; Check to see if we're already past the point were we're allowed
  50. ; to scan in the input string.
  51.  
  52.         cmp    di, cx
  53.         jae    MTiSFailure
  54.  
  55.  
  56. ; If the pattern string is the empty string, always match.
  57.  
  58.         cmp    byte ptr ds:[si], 0
  59.         je    MTSsuccess
  60.  
  61.  
  62. ; The following loop scans through the input string looking for
  63. ; the first character in the pattern string.
  64.  
  65. ScanLoop:    push    si
  66.         lodsb            ;Get first char of string
  67.  
  68.         dec    di
  69. FindFirst:    inc    di        ;Move on to next (or 1st) char.
  70.         cmp    di, cx        ;If at cx, then we've got to
  71.         jae    CantFind1st    ; fail.
  72.  
  73.         mov    ah, es:[di]    ;Get input character.
  74.         cmp    ah, 'a'        ;Convert input character to
  75.         jb    DoCmp        ; upper case if it's a lower
  76.         cmp    ah, 'z'        ; case character.
  77.         ja    DoCmp
  78.         and    ah, 5fh
  79. DoCmp:        cmp    al, ah        ;Compare input character against
  80.         jne    FindFirst    ; pattern string.
  81.  
  82.  
  83. ; At this point, we've located the first character in the input string
  84. ; that matches the first character of the pattern string.  See if the
  85. ; strings are equal.
  86.  
  87.         push    di        ;Save restart point.
  88.  
  89. CmpLoop:    cmp    di, cx        ;See if we've gone beyond the
  90.         jae    StrNotThere    ; last position allowable.
  91.         lodsb            ;Get next input character.
  92.         cmp    al, 0        ;At the end of the parameter
  93.         je    MTSsuccess2    ; string?  If so, succeed.
  94.  
  95.         inc    di
  96.         mov    ah, es:[di]    ;Get the next input character.
  97.         cmp    ah, 'a'        ;Convert input character to
  98.         jb    DoCmp2        ; upper case if it's a lower
  99.         cmp    ah, 'z'        ; case character.
  100.         ja    DoCmp2
  101.         and    ah, 5fh
  102. DoCmp2:        cmp    al, ah        ;Compare input character against
  103.         je    CmpLoop
  104.         pop    di
  105.         pop    si
  106.         jmp    ScanLoop
  107.  
  108.  
  109. StrNotThere:    add    sp, 2        ;Remove di from stack.
  110. CantFind1st:    add    sp, 2        ;Remove si from stack.
  111. MTiSFailure:    pop    si
  112.         pop    di
  113.         mov    ax, di        ;Return failure position in AX.
  114.         popf
  115.         clc            ;Return failure.
  116.         ret
  117.  
  118. MTSSuccess2:    add    sp, 2        ;Remove DI value from stack.
  119. MTSSuccess:    add    sp, 2        ;Remove SI value from stack.
  120.         mov    ax, di        ;Return next position in AX.
  121.         pop    si
  122.         pop    di
  123.         popf
  124.         stc            ;Return success.
  125.         ret
  126. MatchToiStr    endp
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135. Main        proc
  136.         mov    ax, dseg
  137.         mov    ds, ax
  138.         mov    es, ax
  139.         meminit
  140.  
  141.  
  142.         lesi    TestString
  143.         ldxi    TestPat
  144.         xor    cx, cx
  145.         match
  146.         jnc    Quit
  147.         print
  148.         byte    "Matched",cr,lf,0
  149.  
  150.  
  151. Quit:        ExitPgm
  152. Main        endp
  153.  
  154. cseg            ends
  155.  
  156. sseg        segment    para stack 'stack'
  157. stk        db    1024 dup ("stack   ")
  158. sseg        ends
  159.  
  160. zzzzzzseg    segment    para public 'zzzzzz'
  161. LastBytes    db    16 dup (?)
  162. zzzzzzseg    ends
  163.         end    Main
  164.